package com.community.shopping.util;

import java.io.BufferedWriter;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.StringWriter;
import java.io.Writer;
import java.util.Map;
import javax.servlet.ServletContext;
import freemarker.template.Configuration;
import freemarker.template.DefaultObjectWrapper;
import freemarker.template.Template;
import freemarker.template.TemplateException;

/**
 * FreeMarker 操作类
 * 
 * @author Along
 * 
 */
public class FreeMarkerUtil {

	/**
	 * 定义FreeMarker Configuration对象config
	 */
	private static Configuration config = null;

	/**
	 * 编码
	 */
	private static final String FREEMARKER_ENCODING = "utf-8";
	/**
	 * 模板路径
	 */
	private static final String FREEMARKER_PATH = "/ftl";

	/**
	 * FreeMarkerUtil对象freemarker
	 */
	private static FreeMarkerUtil freemarker = new FreeMarkerUtil();

	/**
	 * 初始化config
	 */
	private static void initConfig(ServletContext context) {

		config = new Configuration();
		config.setServletContextForTemplateLoading(context, FREEMARKER_PATH);
		config.setDefaultEncoding(FREEMARKER_ENCODING);
		config.setObjectWrapper(new DefaultObjectWrapper());

	}

	/**
	 * 得到FreeMarkerUtil对象
	 * 
	 * @return FreeMarkerUtil对象
	 */
	public static FreeMarkerUtil getInstance(ServletContext context) {
		if (freemarker.config == null) {
			initConfig(context);
		}
		return freemarker;
	}

	/**
	 * 生成静态页面
	 * 
	 * @param ftlFile
	 *            模板文件名称
	 * @param data
	 *            数据Map
	 * @param file
	 *            生成的文件
	 * @throws IOException
	 * @throws TemplateException
	 */
	public void createHTML(String ftlFile, Map data, File file)
			throws IOException, TemplateException {
		Writer out = null;
		FileOutputStream output = null;
		try {
			Template template = config.getTemplate(ftlFile);
			output = new FileOutputStream(file);
			out = new OutputStreamWriter(output, FREEMARKER_ENCODING);
			template.process(data, out);
		} catch (Exception ex) {
			ex.printStackTrace();
		} finally {
			// 关闭out output
			if (out != null) {
				out.flush();
				out.close();
			}
			if (output != null) {
				output.flush();
				output.close();
			}
		}
	}

	/**
	 * 输出到字符串
	 * 
	 * @param ftlFile
	 *            模板文件名称
	 * @param data
	 *            数据Map
	 * @return 结果字符串
	 */
	public String getString(String ftlFile, Map data) {
		StringWriter writer = null;
		String result = "";
		try {
			Template template = config.getTemplate(ftlFile);
			template.setEncoding(FREEMARKER_ENCODING);
			writer = new StringWriter();
			template.process(data, writer);
			result = writer.toString();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			// 关闭write
			try {
				if (writer != null) {
					writer.flush();
					writer.close();
				}
			} catch (Exception ex) {
				ex.printStackTrace();
			}
		}
		return result;
	}

	/**
	 * 输出到内存
	 * 
	 * @param ftlFile
	 *            模板文件名称
	 * @param data
	 *            数据Map
	 * @return ByteArrayOutputStream
	 * @throws IOException
	 */
	public ByteArrayOutputStream createByteArray(String ftlFile, Map data)
			throws IOException {
		ByteArrayOutputStream os = null;
		BufferedWriter writer = null;
		try {
			Template template = config.getTemplate(ftlFile);
			template.setEncoding(FREEMARKER_ENCODING);
			os = new ByteArrayOutputStream();
			writer = new BufferedWriter(new OutputStreamWriter(os));
			template.process(data, writer);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			// 关闭os write
			if (writer != null) {
				writer.flush();
				writer.close();
			}
			if (os != null) {
				os.flush();
				os.close();
			}
		}
		return os;
	}
}
